package com.mollytask.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mollytask.R;
import com.mollytask.bean.DataCheckList;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.MyUser;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by freshxu on 2017/5/29.
 */

public abstract class BaseCheckListFragment extends Fragment {

    private RecyclerView mRecyclerView;

    protected List<DataCheckList> mDataChecks = new ArrayList<>();

    protected List<DataFill> mDataFills = new ArrayList<>();

    private CheckAdapter mAdapter;

    protected MyUser myUser;

    protected Institution mInstitution;

    protected OnCheckItemClickListener mListener;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), container, false);
        myUser = BmobUser.getCurrentUser(MyUser.class);
        initRecyclerView(view);
        init();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    public abstract int getLayout();

    public abstract boolean isCheck();

    public abstract void init();

    public abstract int getRecyclerViewId();


    protected void initRecyclerView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(getRecyclerViewId());
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, isCheck());
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.check_btn:
                        if (mListener != null) {
                            mListener.onCheckItemClick(position);
                        }
                        break;
                }
            }
        });


    }

    private void initData() {

        mDataChecks.clear();
        mDataFills.clear();


        //在数据中查找上级行政行政机构是自己的填报记录,并且未审核的
        BmobQuery<DataFill> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("mSuperCode", myUser.getCode());
        bmobQuery.order("-createdAt");
        if (isCheck()) {
            bmobQuery.addWhereEqualTo("status", 0);
        } else {
            bmobQuery.addWhereNotEqualTo("status", 0);

        }
        bmobQuery.findObjects(new FindListener<DataFill>() {
            @Override
            public void done(List<DataFill> list, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < list.size(); i++) {
                        DataFill dataFill = list.get(i);
                        DataCheckList dataCheckList = new DataCheckList();
                        dataCheckList.setName(dataFill.getmInName());
                        String fillId = dataFill.getmFillId();
                        String data = fillId.substring(9, 13) + "年第" + fillId.substring(13, 14) + "季度";
                        dataCheckList.setData(dataFill.getmFillId());
                        dataCheckList.setData(data);
                        dataCheckList.setStatus(dataFill.getStatus().toString());
                        //添加至集合中
                        mDataChecks.add(dataCheckList);
                        mDataFills.add(dataFill);
                    }
                    if (mAdapter == null) {
                        mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, false);
                        mRecyclerView.setAdapter(mAdapter);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    public interface OnCheckItemClickListener {
        void onCheckItemClick(int position);
    }

    public void setOnCheckItemClickListener(OnCheckItemClickListener onCheckItemClickListener) {
        mListener = onCheckItemClickListener;
    }


}
